package com.roy.kafka;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.IBasicBolt;
import org.apache.storm.topology.IRichBolt;
import org.apache.storm.topology.IWindowedBolt;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Tuple;

import java.util.Map;

public class MyKafkaBolt extends BaseRichBolt {
    private OutputCollector collector;
    @Override
    public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {
        this.collector = outputCollector;
    }

    @Override
    public void execute(Tuple tuple) {
        //收到的消息结构为 [track, 0, 3, null, abc] topic,partition,offset,key,value
        System.err.println("MyKafkaBolt received tuple = " +tuple);
        String topic = tuple.getString(0);
        String kafkaMsg =tuple.getString(4);
        System.err.println("MyKafkaBolt topic => "+topic+" kafkaMsg ==> "+kafkaMsg);
//        System.err.println("MyKafkaBolt kafkaMsg ==> "+kafkaMsg);
        collector.ack(tuple);//声明该tuple消费成功，避免重复消费。
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
    }
}
